﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// qt headers
#include <QSize>
#include <QFontMetrics>

// custom headers
#include "CustomTreeModel.h"
#include "CustomTreeItem.h"

#include "DebugMessage.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs
{
    CustomTreeModel::CustomTreeModel(QObject *parent /*= nullptr*/) :
        QAbstractItemModel(parent),
        m_spRootItem(std::make_shared<CustomTreeItem>(QList<QVariant>()))
    {

    }

    CustomTreeModel::~CustomTreeModel()
    {

    }

    QVariant CustomTreeModel::data(const QModelIndex & index, int role) const
    {
        if (!index.isValid())
        {
            return QVariant();
        }

        CustomTreeItem *item = static_cast<CustomTreeItem*>(index.internalPointer());
        DBG_WARN_AND_RETURN_UNLESS(item, QVariant(), L"item 为空", L"GMEP", L"2024-01-30");
        QFont font;
        font.setFamily("Microsoft YaHei");
        font.setPixelSize(12);
        switch (role)
        {
        case Qt::DisplayRole:
        {
            return item->Data(index.column());
        }
        case Qt::ToolTipRole:
        {
            return item->Data(index.column());
        }
        case Qt::FontRole:
        {
            return font;
        }
        case Qt::SizeHintRole: 
        {
            auto text = item->Data(index.column()).toString();
            QFontMetrics fontMetrics(font);
            auto width = fontMetrics.tightBoundingRect(text).width();
            return QSize(width, 28);
        }
        default:
            break;
        }

        return QVariant();
    }

    Qt::ItemFlags CustomTreeModel::flags(const QModelIndex & index) const
    {
        if (!index.isValid())
        {
            return Qt::NoItemFlags;
        }

        return QAbstractItemModel::flags(index);
    }

    QVariant CustomTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
        if (orientation == Qt::Horizontal)
        {
            if (role == Qt::DisplayRole) 
            {
                DBG_WARN_AND_RETURN_UNLESS(m_spRootItem, QVariant(), L"m_spRootItem 为空", L"GMEP", L"2024-01-30");
                return m_spRootItem->Data(section);
            }
        }
        else if (orientation == Qt::Vertical)
        {
        }

        return QVariant();
    }

    QModelIndex CustomTreeModel::index(int row, int column, const QModelIndex & parent) const
    {
        if (!hasIndex(row, column, parent))
        {
            return QModelIndex();
        }

        CustomTreeItem *parentItem;
        if (!parent.isValid())
        {
            parentItem = m_spRootItem.get();
        }
        else
        {
            parentItem = static_cast<CustomTreeItem*>(parent.internalPointer());
        }
        DBG_WARN_AND_RETURN_UNLESS(parentItem, QModelIndex(), L"parentItem 为空", L"GMEP", L"2024-01-30");
        CustomTreeItem *childItem = parentItem->ChildItem(row);

        if (childItem)
        {
            return createIndex(row, column, childItem);
        }
        else
        {
            return QModelIndex();
        }
    }

    QModelIndex CustomTreeModel::parent(const QModelIndex & index) const
    {
        if (!index.isValid())
        {
            return QModelIndex();
        }

        CustomTreeItem *item = static_cast<CustomTreeItem*>(index.internalPointer());
        DBG_WARN_AND_RETURN_UNLESS(item, QModelIndex(), L"item 为空", L"GMEP", L"2024-01-30");
        CustomTreeItem *parentItem = item->ParentItem();

        if (parentItem == m_spRootItem.get())
        {
            return QModelIndex();
        }
        DBG_WARN_AND_RETURN_UNLESS(parentItem, QModelIndex(), L"parentItem 为空", L"GMEP", L"2024-01-30");
        return createIndex(parentItem->Row(), 0, parentItem);
    }

    int CustomTreeModel::rowCount(const QModelIndex & parent) const
    {
        if (parent.column() > 0)
        {
            return 0;
        }

        CustomTreeItem *parentItem;
        if (!parent.isValid())
        {
            parentItem = m_spRootItem.get();
        }
        else
        {
            parentItem = static_cast<CustomTreeItem*>(parent.internalPointer());
        }
        DBG_WARN_AND_RETURN_UNLESS(parentItem, 0, L"parentItem 为空", L"GMEP", L"2024-01-30");
        return parentItem->ChildCount();
    }

    int CustomTreeModel::columnCount(const QModelIndex & parent) const
    {
        if (!parent.isValid())
        {
            DBG_WARN_AND_RETURN_UNLESS(m_spRootItem, 0, L"m_spRootItem 为空", L"GMEP", L"2024-01-30");
            return m_spRootItem->ColumnCount();
        }
        else
        {
            DBG_WARN_AND_RETURN_UNLESS(parent.internalPointer(), 0, L"parent.internalPointer() 为空", L"GMEP", L"2024-01-30");
            return static_cast<CustomTreeItem*>(parent.internalPointer())->ColumnCount();
        }
    }

    void CustomTreeModel::SetRoot(std::shared_ptr<CustomTreeItem> spRootItem)
    {
        beginRemoveRows(QModelIndex(), 0, rowCount());
        removeRows(0, rowCount());
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_spRootItem, L"m_spRootItem为空", L"GMEP", L"2024-01-30");
        m_spRootItem->RemoveChildren(0, rowCount());
        endRemoveRows();

        m_spRootItem = spRootItem == nullptr ? std::make_shared<CustomTreeItem>(QList<QVariant>()) : spRootItem;
    }
}